package com.yuneec.fly.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.yuneec.fly.R;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.presenters.ConstantPresenter;
import com.yuneec.fly.utils.ChannelUtils;
import com.yuneec.fly.views.KnobsView;
import com.yuneec.fly.views.SlideVernierView;
import com.yuneec.fly.views.ThreeSpeedSwitchView;
import com.yuneec.fly.views.TwoSpeedSwitchView;
import com.yuneec.fly.views.VernierView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class HardwareMonitorFragment extends BaseFragment {
    private KnobsView hm_view_k1;
    private SlideVernierView hm_view_k2, hm_view_k3;
    private TwoSpeedSwitchView hm_view_b1, hm_view_b2, hm_view_b3, hm_view_b4, hm_view_s5;
    private ThreeSpeedSwitchView hm_view_s1, hm_view_s2, hm_view_s3, hm_view_s4;
    private VernierView hm_view_j1, hm_view_j2, hm_view_j3, hm_view_j4;
//	private boolean openTrim;

    @Override
    protected int getContentViewResourceId() {
        return R.layout.system_setting_hardware_monitor_main;
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        hm_view_j1 = (VernierView) selfView.findViewById(R.id.hm_view_j1);
        hm_view_j2 = (VernierView) selfView.findViewById(R.id.hm_view_j2);
        hm_view_j3 = (VernierView) selfView.findViewById(R.id.hm_view_j3);
        hm_view_j4 = (VernierView) selfView.findViewById(R.id.hm_view_j4);

        hm_view_k1 = (KnobsView) selfView.findViewById(R.id.hm_view_k1);
        hm_view_k2 = (SlideVernierView) selfView.findViewById(R.id.hm_view_k2);
        hm_view_k3 = (SlideVernierView) selfView.findViewById(R.id.hm_view_k3);

        hm_view_s1 = (ThreeSpeedSwitchView) selfView.findViewById(R.id.hm_view_s1);
        hm_view_s2 = (ThreeSpeedSwitchView) selfView.findViewById(R.id.hm_view_s2);
        hm_view_s3 = (ThreeSpeedSwitchView) selfView.findViewById(R.id.hm_view_s3);
        hm_view_s4 = (ThreeSpeedSwitchView) selfView.findViewById(R.id.hm_view_s4);
        hm_view_s5 = (TwoSpeedSwitchView) selfView.findViewById(R.id.hm_view_s5);

        hm_view_b1 = (TwoSpeedSwitchView) selfView.findViewById(R.id.hm_view_b1);
        hm_view_b2 = (TwoSpeedSwitchView) selfView.findViewById(R.id.hm_view_b2);
        hm_view_b3 = (TwoSpeedSwitchView) selfView.findViewById(R.id.hm_view_b3);
        hm_view_b4 = (TwoSpeedSwitchView) selfView.findViewById(R.id.hm_view_b4);

        ((ImageView) selfView.findViewById(R.id.hm_image_marked)).setImageResource(R.drawable.system_setting_hardware_monitor_iamge_marked);
    }

    @Override
    public void onResume() {
        super.onResume();
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new EnterSim(),null));
//        activity.mControllerService.send(Message.obtain(null, UARTController.CORRECT_TXSTATE, UARTController.STATE_SIM, 3));
    }

    @Override
    public void onPause() {
        super.onPause();
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new ExitSim(),null));
//        activity.mControllerService.send(Message.obtain(null, UARTController.CORRECT_TXSTATE, UARTController.STATE_AWAIT, 3));
    }

    public void updateReceiveChannels() {
        int[] channles = ConstantPresenter.getInstance().getChannels();
        if (channles == null) {
            return;
        }
        hm_view_j1.setValue(vernieDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_J + 1)));
        hm_view_j2.setValue(vernieDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_J + 2)));
        hm_view_j3.setValue(vernieDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_J + 3)));
        hm_view_j4.setValue(vernieDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_J + 4)));
        hm_view_k1.setValue(knobsDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_K + 1)));
        hm_view_k2.setValue(slideDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_K + 2)));
        hm_view_k3.setValue(slideDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_K + 3)));
        hm_view_s1.setValue(threeSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_S + 1)));
        hm_view_s2.setValue(threeSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_S + 2)));
        hm_view_s3.setValue(threeSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_S + 3)));
        hm_view_s4.setValue(threeSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_S + 4)));
        hm_view_s5.setValue(twoSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_S + 5)));
        hm_view_b1.setValue(twoSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_B + 1)));
        hm_view_b2.setValue(twoSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_B + 2)));
        hm_view_b3.setValue(twoSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_B + 3)));
        hm_view_b4.setValue(twoSwitchDataConversion(ChannelUtils.getChannelValue(channles, ChannelUtils.HW_INDEX_BASE_B + 4)));
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBusFactory.getFeedBackBus().isRegistered(this)) {
            EventBusFactory.getFeedBackBus().register(this);
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        if (EventBusFactory.getFeedBackBus().isRegistered(this)) {
            EventBusFactory.getFeedBackBus().unregister(this);
        }
    }

    private int knobsDataConversion(float data) {
        float per_degree_value = 4096f / 270f;
        int degree = 0;

        data = (data > 4095f) ? 4095f : data;
        data = (data < 0) ? 0 : data;

        degree = (int) (data / per_degree_value);

        int result = (int) ((225 + degree) % 360);
        return result;
    }

    private int vernieDataConversion(float data) {
        float per_calibration_value = 4096f / 200f;
        int calibration = 0;

        data = (data > 4095f) ? 4095f : data;
        data = (data < 0) ? 0 : data;
        calibration = (int) ((data / per_calibration_value) - 100);
        return calibration;
    }

    private int slideDataConversion(float data) {
        float per_degree = 4096f / 200f;
        int degree = 0;

        data = (data > 4095f) ? 4095f : data;
        data = (data < 0) ? 0 : data;

        degree = (int) (data / per_degree);

        return degree;
    }

    private int vernieTrimDataConversion(float data) {
        int value;

        data = (data > 20f) ? 20f : data;
        data = (data < -20f) ? -20f : data;

        value = (int) data;
        return value;
    }

    private int threeSwitchDataConversion(float data) {
        int three_switch = 0;

        data = (data > 2f) ? 2f : data;
        data = (data < 0) ? 0 : data;

        three_switch = (int) (data);

        return three_switch;
    }

    private int twoSwitchDataConversion(float data) {
        int two_switch = 0;

        data = (data > 1f) ? 1f : data;
        data = (data < 0) ? 0 : data;

        two_switch = (int) (data);

        return two_switch;
    }
}
